package com.smart.tv.boundary;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.smart.tv.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

public class GalleryMenu extends Activity{

	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery_menu);
		fillAllMenu();
		Intent intent = getIntent();
		statusMenu = intent.getStringExtra(STATUS_MENU);
		TextView txtTitle = (TextView) findViewById(R.id.title);
		txtTitle.setText(statusMenu);
		final ImageView image = (ImageView) findViewById(R.id.imageView1);
		Gallery gallery = (Gallery) findViewById(R.id.gallery);
		gallery.setAdapter(new GalleryAdapter(this, R.layout.gallery_item));
		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				image.setImageResource(daftarMenu.get(statusMenu).get(arg2));
			}

			public void onNothingSelected(AdapterView<?> arg0) {				
			}
		});
		if(daftarMenu!=null&&daftarMenu.get(statusMenu)!=null){
			gallery.setSelection(daftarMenu.get(statusMenu).size()/2);
		}
		gallery.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				if(position == 0){
					if(statusMenu.equals(MOVIE)){
						String movieurl = Environment.getExternalStorageDirectory() + "/Video/darknight.3gp";

						Intent intentToPlayVideo = new Intent(Intent.ACTION_VIEW);
						intentToPlayVideo.setDataAndType(Uri.parse(movieurl), "video/*");
						startActivity(intentToPlayVideo);
					}else if (statusMenu.equals(GAME)){
						Intent i = new Intent(Intent.ACTION_MAIN);
						PackageManager manager = getPackageManager();
						i = manager.getLaunchIntentForPackage("com.miniclip.hambo");
						i.addCategory(Intent.CATEGORY_LAUNCHER);
						startActivity(i);
					}else if (statusMenu.equals(MUSIC)){
						Intent intent = new Intent();  
						intent.setAction(android.content.Intent.ACTION_VIEW);  
						File file = new File("");  
						intent.setDataAndType(Uri.fromFile(file), "audio/*");  
						startActivity(intent);
					}
				}else{
					if(statusMenu.equals(MOVIE)){
						String movieurl = Environment.getExternalStorageDirectory() + "/Video/expendables.3gp";

						Intent intentToPlayVideo = new Intent(Intent.ACTION_VIEW);
						intentToPlayVideo.setDataAndType(Uri.parse(movieurl), "video/*");
						startActivity(intentToPlayVideo);
					}else if (statusMenu.equals(GAME)){
						Intent i = new Intent(Intent.ACTION_MAIN);
						PackageManager manager = getPackageManager();
						i = manager.getLaunchIntentForPackage("com.miniclip.istunt2");
						i.addCategory(Intent.CATEGORY_LAUNCHER);
						startActivity(i);
					}else if (statusMenu.equals(MUSIC)){
						Intent intent = new Intent();  
						intent.setAction(android.content.Intent.ACTION_VIEW);  
						File file = new File("");  
						intent.setDataAndType(Uri.fromFile(file), "audio/*");  
						startActivity(intent);
					}
				}
				
				
			}
		});
		
	}
	
	private void fillAllMenu(){
		List<Integer> channelMenu = new ArrayList<Integer>();
		channelMenu.add(R.drawable.channel_indosiar);
		channelMenu.add(R.drawable.channel_trans7);
		channelMenu.add(R.drawable.channel_transtv);
		daftarMenu.put(CHANNEL, channelMenu);
		
		List<Integer> koranMenu = new ArrayList<Integer>();
		koranMenu.add(R.drawable.koran_kompas);
		koranMenu.add(R.drawable.koran_mediaindonesia);
		koranMenu.add(R.drawable.koran_sindo2);
		daftarMenu.put(KORAN, koranMenu);
		
		List<Integer> movieMenu = new ArrayList<Integer>();
		movieMenu.add(R.drawable.movie_darkknight);
		movieMenu.add(R.drawable.movie_expendables);
		daftarMenu.put(MOVIE, movieMenu);
		
		List<Integer> gameMenu = new ArrayList<Integer>();
		gameMenu.add(R.drawable.game_angry_bird);
		gameMenu.add(R.drawable.game_pizza_maker);
		daftarMenu.put(GAME, gameMenu);
		
	}

	public static final String CHANNEL 	= "Channel";
	public static final String KORAN 	= "Koran";
	public static final String LAYANAN 	= "Layanan";
	public static final String MOVIE 	= "Film";
	public static final String FOOD 	= "Makanan";
	public static final String DRINK 	= "Minuman";
	public static final String GAME		= "game";
	public static final String MUSIC	= "music";
	
	public static final String STATUS_MENU = "statusMenu";
	
	private String statusMenu = "";
	
	private Map<String, List<Integer>> daftarMenu = new HashMap<String, List<Integer>>();
	
	
	private class GalleryAdapter extends BaseAdapter{

		public GalleryAdapter(Context contextParam, int resourceIdParam){
			context = contextParam;
			resourceId = resourceIdParam;
		}
		public int getCount() {
			if(daftarMenu == null) return 0;
			if(daftarMenu.get(statusMenu) == null) return 0;
			return daftarMenu.get(statusMenu).size();
		}

		public Object getItem(int arg0) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			
			if(daftarMenu ==null)return null;
			List<Integer> menus = daftarMenu.get(statusMenu);
			if(menus == null) return null;
			if (row == null) {
				LayoutInflater inflater = (LayoutInflater) this.context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				
				row = inflater.inflate(resourceId, parent, false);
			}
			
			ImageView icon	= (ImageView) row.findViewById(R.id.listMenuImage);
			icon.setScaleType(ImageView.ScaleType.FIT_XY);
			icon.setBackgroundColor(Color.GRAY);
			icon.setImageResource(menus.get(position));
			Log.d("banyak item", menus.size()+" items");
			
			return row;
		}
		
		private int resourceId = 0;
		private Context context = null;
		
	}
}
